home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
CUGUK
/
PROG_TOO
/
C013.ZIP
/
HD.C
< prev
next >
Wrap
Text File
|
1990-01-19
|
2KB
|
105 lines
/********************************************************************
* C Users Group (U.K) C Source Code Library File CUGLIB.013 *
* Inquiries to: M. Houston, 36 Whetstone Clo. Farquhar Rd. *
* Edgbaston, Birmingham B15 2QN ENGLAND *
********************************************************************
* File name: hd.c
* Program name:hd
* Source of file: Ron Wellstead
* Purpose: An MS-DOS copy of the UNIX utility of the same name.
* Changes: <who what when & why major changes have been made>
********************************************************************/
/*
*
* @(#) hd.c 1.2 87/07/27
*
* UNIX style hd utility for dos
*
* copyright (c) 1987 Ron Wellsted.
* This software is provided on the understanding that it is
* NOT to be used for commercial gain. It may be freely distributed
* in source or object form among amateur and hobby computer users ONLY!
*
* sends a hexadecimal dump of files (or stdin) to stdout
* usage: hd [files....]
* written for Microsoft C, link with setargv.obj to expand wildcards
*/
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>
#include <ctype.h>
char buff[17];
char what[]="@(#) hd VR 1.0.0 15 Jul 1987";
#define FALSE 0
#define TRUE 1
long addr;
main(argc, argv)
int argc;
char **argv;
{
int fh, prn_name=FALSE;
if (argc == 1) /* no args; copy standard input */
filecopy(fileno(stdin));
else
{
if (argc>2) prn_name=TRUE;
while (--argc > 0)
{
if ((fh = open(*++argv, O_BINARY | O_RDONLY)) < 0)
fprintf(stderr,"hd: Can't open %s\n",*argv);
else
{
if (prn_name)
printf("\nhd %s",*argv);
filecopy(fh);
close(fh);
}
}
}
}
filecopy(fh) /* copy file fh to standard output */
int fh;
{
char ch,outline[80];
int col,i,j,n;
addr=0L;
lseek(fh,0L,0);
do
{
j = read(fh,buff,16);
while (j<16)
{
n = 16-j;
n = read(fh,&buff[j],n);
if (n==0) break;
j += n;
}
col = sprintf(outline,"%04lx: ",addr);
for (i=0; i<j; ++i)
{
col += sprintf(&outline[col],"%02x ",0xff & buff[i]);
++addr;
}
while (col<55) col += sprintf(&outline[col]," ");
for (i=0; i<j; i++)
if (!isprint(buff[i])) buff[i] = '.';
buff[i]='\0';
strcat(outline,buff);
printf("%s\n",outline);
}
while (j==16);
if (j) printf("%04lx: ",addr);
printf("\n");
}